'''
基于Session和Cookie模拟登录并爬取数据
'''

# import requests
# from urllib.parse import urljoin
#
# BASE_URL = 'https://login2.scrape.center/'
# LOGIN_URL = urljoin(BASE_URL, 'login')
# INDEX_URL = urljoin(BASE_URL, 'page/1')
# USERNAME = 'admin'
# PASSWORD = 'admin'
#
# response_login = requests.post(LOGIN_URL, data={
#     'username': USERNAME,
#     'password': PASSWORD
# }, allow_redirects=False)
# cookies = response_login.cookies
# print('Cookies', cookies)
# response_index = requests.get(INDEX_URL, cookies=cookies)
# print('Response Status', response_index.status_code)
# print('Response URL', response_index.url)

# 类比上面代码更简便的写法
# import requests
# from urllib.parse import urljoin
#
# BASE_URL = 'https://login2.scrape.center/'
# LOGIN_URL = urljoin(BASE_URL, 'login')
# INDEX_URL = urljoin(BASE_URL, 'page/1')
# USERNAME = 'admin'
# PASSWORD = 'admin'
#
# session = requests.Session()
# response_login = session.post(LOGIN_URL, data={
#     'username': USERNAME,
#     'password': PASSWORD
# })
# cookies = session.cookies
# print('Cookies', cookies)
#
# response_index = session.get(INDEX_URL)
# print('Response Status', response_index.status_code)
# print('Response URL', response_index.url)


# 使用selenium模拟登录
from urllib.parse import urljoin
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import requests
import time

BASE_URL = 'https://login2.scrape.center/'
LOGIN_URL = urljoin(BASE_URL, 'login')
INDEX_URL = urljoin(BASE_URL, 'page/1')
USERNAME = 'admin'
PASSWORD = 'admin'
driver_path = 'D:\install\Python\Scripts\chromedriver.exe'

browser = webdriver.Chrome(service=Service(driver_path))
browser.get(BASE_URL)
browser.find_element(By.CSS_SELECTOR,'input[name="username"]').send_keys(USERNAME)
browser.find_element(By.CSS_SELECTOR,'input[name="password"]').send_keys(PASSWORD)
browser.find_element(By.CSS_SELECTOR,'input[type="submit"]').click()
time.sleep(10)

# 从浏览器对象中获取Cookie信息
cookies = browser.get_cookies()
print('Cookies', cookies)
browser.close()

# 把Cookie信息放入请求中
session = requests.Session()
for cookie in cookies:
    session.cookies.set(cookie['name'], cookie['value'])

response_index = session.get(INDEX_URL)
print('Response Status', response_index.status_code)
print('Response URL', response_index.url)